package com.ldzy.user;

import com.ldzy.common.pojo.User;
import com.ldzy.user.config.GetConfig;
import com.ldzy.user.config.JwtProperties;
import com.ldzy.user.utils.JwtTool;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequiredArgsConstructor//会把final属性进行注入
public class MyController {
    private final JwtTool jwtTool;
    private final JwtProperties jwtProperties;
    private final GetConfig myStr; //值在配置中心的配置文件中

    @PostMapping("/user/login")
    public String demo(@RequestBody User user, HttpServletResponse response){
        String token=null;
        if(123456789==user.getId()&&"1234".equals(user.getPwd())){//模拟用户id和密码正确
            //1.生成token
            token = jwtTool.createToken(user.getId(), jwtProperties.getTokenTTL());
            //2.把token添加到返回消息的头部
            response.addHeader("authorization",token);
        }
        //3.返回消息
        return "user,8005";
    }

    @GetMapping("/user/hello")
    public String getHello(){
        //返回一个字符串，但是这个字符串的值放在配置中心的服务器中
        return myStr.getMyStr();
    }
}
